<?php


namespace App\Models\CacheModel\zl1_relation;


use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Zl1AstrCommunityArticleZan extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl1_astr_community_article_zan';

    protected $cacheModel = 'astr_community.article_zan';

    /**
     * 设置 星座讨论 赞同与反对
     * @param int $uid
     * @param int $aid
     * @param int $status
     */
    public function setZan(int $uid, int $aid, int $status)
    {
        try
        {
            $id = $this->where(['uid' => $uid, 'aid' => $aid])->value('id');

            if ($id)
            {
                $this->where('id', $id)->update(['status' => $status, 'update_time' => time()]);
            } else
            {
                $id = $this->insertGetId([
                    'uid' => $uid,
                    'aid' => $aid,
                    'status' => $status,
                    'create_time' => time()
                ]);
            }

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setCache([$aid => $status]);

            return true;
        }
        catch (\Exception $e)
        {
            Log::write('Zl1AstrCommunityArticleZan model => setZan()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }
    }

    /**
     * 获取 星座讨论 赞同与反对
     * @param int $uid
     * @param array $field
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getZan(int $uid, array $field = [])
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->getDataAndSet(function () use ($uid)
        {
            try
            {
                return $this->where(['uid' => $uid])->column('status', 'aid');
            }
            catch (\Exception $e)
            {
                Log::write('Zl1AstrCommunityArticleZan model => getZan()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
                return [];
            }
        }, $field);

        return $data;
    }
}
